//获取应用实例
var app = getApp();
var httpClient = require('../../utils/httpClient');
var route = require('../../utils/route');
var wxpay = require('../../utils/wxpay');
var util = require('../../utils/util');

Page({
  data: {
    quesNo: '',
    user: '',
    quesName: '',
    tradeNo: null,
    amount: '',
    buttonFlag: true,
    mask: 1,
    bannerImage: '/images/banner4.png',
    questionsType: '' // 谜语=1，成语=2，急转弯=3，其他=4,儿童=5，邂逅语=6，诗词=7
  },
  // 事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function(options) {

  },
  onShow: function() {
    var userInfo = wx.getStorageSync('user');
    if (userInfo == null || userInfo.imgUrl == null || userInfo.imgUrl == '') {
      wx.redirectTo({
        url: '../authorize/authorize'
      })
    }
    debugger
    if (app.globalData.titleCassification) { 
      if (app.globalData.titleCassification == 1) {
        this.setData({
          bannerImage: '/images/banner1.png'
        })
      } else if (app.globalData.titleCassification == 2) {
        this.setData({
          bannerImage: '/images/banner2.png'
        })
      } else if (app.globalData.titleCassification == 3) {
        this.setData({
          bannerImage: '/images/banner3.png'
        })
      } else {
        this.setData({
          bannerImage: '/images/banner4.png'
        })
      }
    }
    var that = this;
    var value = wx.getStorageSync('mask');
    that.setData({
      mask: value
    });

    app.getUserInfo(function(res) {
      debugger
      var quesNo = app.globalData.quesNo;
      var quesName = app.globalData.quesName;
      if (quesNo != '' && quesName != '') {
        that.setData({
          quesNo: quesNo,
          quesName: quesName
        });
      } else {
        that.topiA();
      }
      that.setData({
        userInfo: res
      });
      that.getAccount();

    });

  },
  name: function(event) {
    wx.navigateTo({
      url: '../myrecord/myrecord'
    })
  },
  order: function(event) {
    var formId = event.detail.formId;
    console.log(formId);
    debugger
    var that = this;
    if (!that.data.buttonFlag) {
      return;
    }
    that.setData({
      buttonFlag: false
    });

    var url = route.prepay();
    if (that.data.amount === '') {
      wx.showToast({
        title: '请输入金额',
        icon: 'none',
        duration: 3000
      })
      that.setData({
        buttonFlag: true
      });
      return;
    }

    if (!util.checkNumber(that.data.amount)) {
      wx.showToast({
        title: '请输入正确金额',
        icon: 'none',
        duration: 1000
      })
      that.setData({
        buttonFlag: true
      });
      return;
    }

    if (that.data.amount < 1) {
      wx.showToast({
        title: '金额必须大于1元',
        icon: 'none',
        duration: 1000
      })
      that.setData({
        buttonFlag: true
      });
      return;
    }
    if (that.data.amount > 200) {
      wx.showToast({
        title: '最大为200元',
        icon: 'none',
        duration: 1000
      })
      that.setData({
        buttonFlag: true
      });
      return;
    }

    setTimeout(function() {
      debugger
      that.setData({
        buttonFlag: true
      });
    }, 2000);

    var params = {
      userId: that.data.userInfo.id,
      questionsId: that.data.quesNo,
      payAmount: that.data.amount * 100,
      formId: formId
    };
    httpClient.request(url, 'POST', params, function(res, err) {
      var payType = res.payType;
      that.setData({
        buttonFlag: true
      });
      if (payType == 2) {
        that.setData({
          tradeNo: res.resMessage,
          amount: ''
        });
        var payInfo = res;
        var payParams = {
          timeStamp: payInfo.timeStamp,
          nonceStr: payInfo.nonceStr,
          "package": payInfo.packageValue,
          signType: payInfo.signType,
          paySign: payInfo.paySign
        };
        console.log(payInfo);
        wxpay.requestPayment(payParams, that.paySuccess, that.payFail, that.payComplete);
      } else {
        that.setData({
          tradeNo: res.resMessage,
          amount: ''
        });
        wx.navigateTo({
          url: '../answer/answer?tradeNo=' + that.data.tradeNo
          //url: '../guesspacket/guesspacket?tradeNo=' + that.data.tradeNo

        });
        // that.topiA();
      }
    });
  },
  paySuccess: function() {
    var that = this;
    that.setData({
      amount: ''
    });
    console.log("支付成功");
    that.queryPayOrderInfo();
    that.topiA();

  },
  payFail: function() {
    console.log("支付失败");
  },
  payComplete: function(res) {
    var that = this;
    console.log("支付完成" + res);
  },
  queryPayOrderInfo: function() {
    var that = this;
    var method = route.queryPayOrderInfo();
    debugger
    var params = {
      tradeNo: that.data.tradeNo,
    };
    httpClient.request(method, 'GET', params, function(res, err) {
      debugger
      wx.navigateTo({
        //url: '../guesspacket/guesspacket?tradeNo=' + that.data.tradeNo
        url: '../answer/answer?tradeNo=' + that.data.tradeNo
      });
    });
  },

  getAccount: function() {
    var that = this;
    var method = route.getAccount();
    debugger
    var params = {
      openId: app.globalData.openId,
    };
    httpClient.request(method, 'GET', params, function(res, err) {
      debugger
      that.setData({
        user: res,
        // bannerImage: res.bannerImage
      });
    });
  },

  topiA: function() {
    var that = this;
    var method = route.getRandomHbQuestions();
    var params = {

    };
    httpClient.request(method, 'GET', params, function(res, err) {
      that.setData({
        quesNo: res.id,
        quesName: res.questionsName,
        questionsType: res.questionsType
      });
      app.globalData.quesNo = res.id;
      app.globalData.quesName = res.questionsName;
      app.globalData.questionsType = res.questionsType
      debugger
      if (res.titleCassification == 1) {
        that.setData({
          bannerImage: '/images/banner1.png'
        })
      } else if (res.titleCassification == 2) {
        that.setData({
          bannerImage: '/images/banner2.png'
        })
      } else if (res.titleCassification == 3) {
        that.setData({
          bannerImage: '/images/banner3.png'
        })
      } else {
        that.setData({
          bannerImage: '/images/banner4.png'
        })
      }
    });
  },
  mask: function(e) {
    debugger
    var that = this;
    wx.setStorage({
      key: "mask",
      data: 1
    });
    var value = wx.getStorageSync('mask');
    that.setData({
      mask: value
    });
  },
  getAmount: function(e) {
    if (e.detail.value === '') {
      this.setData({
        amount: ''
      })
    }
    if (!util.checkNumber(e.detail.value)) {
      wx.showToast({
        title: '请输入正确金额',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    this.setData({
      amount: e.detail.value,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  topic: function(event) {
    wx.navigateTo({
      url: '../topicchoose/topicchoose'
    })
  },
  txAmount: function() {
    wx.navigateTo({
      url: '../widthdraw/widthdraw'
    })
  }
})